package com.kx.web;

import com.kx.entity.Goods;
import com.kx.entity.GoodsCategory;
import com.kx.service.GoodsCategoryService;
import com.kx.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @RequestMapping("/index")
    public Object home() {
        return "index";
    }

    @RequestMapping("jump")
    public Object jump(String url){
        if (url.contains("goods_edit")){
            int lastIndex=url.length();
            int gid= Integer.parseInt(url.substring(10,lastIndex));

            Goods goods = goodsService.findGoodById(gid);
            GoodsCategory goodsCategory = goodsCategoryService.findGoodCategoryById(goods.getCategoryleveloneId());
            List<GoodsCategory> goodsCategoryList=goodsCategoryService.findGoodsCategoryList();
            ModelAndView mv = new ModelAndView();
            mv.addObject("goods", goods);
            mv.addObject("goodsCategory", goodsCategory);
            mv.addObject("goodsCategoryList",goodsCategoryList);
            mv.setViewName("goods_edit");
            return mv;
        }
        return url;
    }
}
